#include "StdAfx.h"
#include "PropItemTools.h"

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
PropItemTools::PropItemTools(void)
{
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
PropItemTools::~PropItemTools(void)
{
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::GetItemByTitle(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* &subItem, CString title)
{
    if (wndPropList.GetSafeHwnd() == NULL) return FALSE;
    int num = wndPropList.GetPropertyCount();
    for (int i = 0; i < num; i++)
    {
        CMFCPropertyGridProperty* prop = wndPropList.GetProperty(i);

        int mn = prop->GetSubItemsCount();
        for (int i = 0; i < mn; i++)
        {
            CMFCPropertyGridProperty* sub = prop->GetSubItem(i);
            CString l = sub->GetName();

            if (l == title)
            {
                subItem = sub;
                return TRUE;
            }
        }
    }

    return FALSE;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::SetItemEnable(CMFCPropertyGridCtrl &wndPropList, CString title, BOOL bEnable)
{
    CMFCPropertyGridProperty* sub;
    if (GetItemByTitle(wndPropList, sub, title))
    {
        sub->Enable(bEnable);
        return TRUE;
    }

    return FALSE;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::SetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, COleVariant stt)
{
    CMFCPropertyGridProperty* sub;
    if (GetItemByTitle(wndPropList, sub, title))
    {
        sub->SetValue(stt);
        return TRUE;
    }

    return FALSE;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::SetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, int da)
{
    COleVariant ttt((_variant_t)da);
    return SetItemValue(wndPropList, title, ttt);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::SetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, bool da)
{    
    COleVariant ttt((_variant_t)da);   
    return SetItemValue(wndPropList, title, ttt);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::SetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, CString cs)
{
    COleVariant ttt(cs);
    return SetItemValue(wndPropList, title, ttt);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::SetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, double da)
{
    COleVariant ttt((_variant_t)da);
    return SetItemValue(wndPropList, title, ttt);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::GetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, int &da)
{
    COleVariant ttt;
    BOOL stat = GetItemValue(wndPropList, title, ttt);
    CString str = (CString)ttt;
    int mn = str.Find(_T(" "), 0);
    if (mn > 0) str = str.Left(mn);
    da = atoi(str);
    return stat;
}

BOOL PropItemTools::GetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, bool &da)
{
    COleVariant ttt;
    BOOL stat = GetItemValue(wndPropList, title, ttt);
    da = ttt.boolVal;
    return stat;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::GetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, double &da)
{
    COleVariant ttt;
    BOOL stat = GetItemValue(wndPropList, title, ttt);
    CString str = (CString)ttt;
    int mn = str.Find(_T(" "), 0);
    if (mn > 0) str = str.Left(mn);
    da = atof(str);
    return stat;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::GetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, CString &da)
{
    COleVariant ttt;
    BOOL stat = GetItemValue(wndPropList, title, ttt);
    da = (CString)ttt;

    return stat;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
BOOL PropItemTools::GetItemValue(CMFCPropertyGridCtrl &wndPropList, CString title, COleVariant &stt)
{
    CMFCPropertyGridProperty* sub;
    if (GetItemByTitle(wndPropList, sub, title))
    {
        stt = sub->GetValue();
        return TRUE;
        //COleVariant sttOrg = sub->GetOriginalValue();
    }
    return FALSE;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddComboSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, CString csPara)
{
    CString csDefault, csDesc,csOption;
    CArray<CString> csOptions;

    int mn = csPara.Find(_T(","), 0);
    if (mn != -1)
    {
        csDefault = csPara.Left(mn);
        csPara = csPara.Mid(mn+1);
    }
    else return;

    int id1 = csPara.Find(_T("<"), 0);
    int id2 = csPara.Find(_T(">"), 0);
    if (id1 != -1 && id2!=-1)
    {
        csOption = csPara.Mid(id1+1,id2-id1-1);
        csPara = csPara.Mid(id2+1);
    }
    else return;

    CString csSingle;
    while (TRUE)
    {
        int mn = csOption.Find(_T(","), 0);
        if (mn == -1)
        {
            csOptions.Add(csOption);
            break;
        }

        csSingle = csOption.Left(mn);

        csOptions.Add(csSingle);
        csOption = csOption.Mid(mn + 1);
    }

    id1 = csPara.Find(_T("["), 0);
    id2 = csPara.Find(_T("]"), 0);
    if (id1 != -1 && id2 != -1)
    {
        csDesc = csPara.Mid(id1 + 1, id2 - id1 - 1);
    }
    else return;

    PropItemTools::AddComboStringSubItem(wndPropList, pGroup, csName, csDefault, csOptions, csDesc);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddSpinSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, int iDefaultValue, int min, int max, CString csDesc)
{
    CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(csName, (_variant_t)iDefaultValue, csDesc);
    pProp->EnableSpinControl(TRUE, min, max);

    pGroup->AddSubItem(pProp);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddBoolSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, bool bDefault, CString csDesc)
{
    pGroup->AddSubItem(new CMFCPropertyGridProperty(csName, (_variant_t)bDefault, csDesc));
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddDoubleSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, double dDefault, CString csDesc)
{
    pGroup->AddSubItem(new CMFCPropertyGridProperty(csName, (_variant_t)dDefault, csDesc));
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddIntSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, int iDefault, CString csDesc)
{
    pGroup->AddSubItem(new CMFCPropertyGridProperty(csName, (_variant_t)iDefault, csDesc));
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddStringSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, CString Default, CString csDesc)
{
    pGroup->AddSubItem(new CMFCPropertyGridProperty(csName, (_variant_t)Default, csDesc));
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddComboStringSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, CString csDefault, CArray<CString> &csOptions, CString csDesc)
{
    CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(csName, csDefault, csDesc);
    for (int i = 0; i < csOptions.GetSize(); i++)
    {
        pProp->AddOption(csOptions[i]);
    }

    pGroup->AddSubItem(pProp);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddComboIntSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, int iDefault, int min, int max, int add, CString csDesc)
{
    CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(csName, (_variant_t)iDefault, csDesc);

    CArray<CString> csOptions;
    GetOptionsStringByData(min, max, add, csOptions);

    for (int i = 0; i < csOptions.GetSize(); i++)
    {
        pProp->AddOption(csOptions[i]);
    }

    pGroup->AddSubItem(pProp);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::AddComboDoubleSubItem(CMFCPropertyGridCtrl &wndPropList, CMFCPropertyGridProperty* pGroup, CString csName, double iDefault, double min, double max, double add, CString csDesc)
{
    CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(csName, (_variant_t)iDefault, csDesc);

    CArray<CString> csOptions;
    GetOptionsStringByData(min, max, add, csOptions);

    for (int i = 0; i < csOptions.GetSize(); i++)
    {
        pProp->AddOption(csOptions[i]);
    }

    pGroup->AddSubItem(pProp);
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::GetOptionsStringByData(int min, int max, int add, CArray<CString> &csOptions)
{
    csOptions.RemoveAll();
    CString cs;
    int id = 0;
    for (int da = min; da < max + 1; da += add)
    {
        //if (id == 0)
        //{
            cs.Format(_T("%d"), da);
        //}
        //else
        //{
        //    cs.Format(_T(",%d"), da);
        //}

        id++;

        csOptions.Add(cs);
    }
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
int PropItemTools::GetOptionItemCurSel(CMFCPropertyGridCtrl &wndPropList, CString title)
{
    int id = -1;
    CMFCPropertyGridProperty* sub;
    if (PropItemTools::GetItemByTitle(wndPropList, sub, title))
    {
        COleVariant stt = sub->GetValue();

        CString csType = (CString)stt.bstrVal;

        int nm = sub->GetOptionCount();
        for (int i = 0; i < nm; i++)
        {
            CString cs = sub->GetOption(i);
            if (cs == csType)
            {
                id = i;
                break;
            }
        }
    }

    return id;
}

//---------------------------------------------------------------------------------------
// @bsimethod                                              Y.Dong          11/2014
//---------------------------------------------------------------------------------------
void PropItemTools::SetOptionItemCurSel(CMFCPropertyGridCtrl &wndPropList, CString title, int id)
{
    CMFCPropertyGridProperty* sub;
    if (PropItemTools::GetItemByTitle(wndPropList, sub, title))
    {
        int nm = sub->GetOptionCount();
        if (id >= 0 && id < nm)
        {
            CString cs = sub->GetOption(id);
            PropItemTools::SetItemValue(wndPropList, title, cs);
        }
    }
}
